package org.rupa.common.model;

import lombok.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.net.Inet4Address;
import java.net.InetAddress;

@ConfigurationProperties(prefix = "rupa-serve")
@Component
@Data
public class RegistryCenterInfo {
    private String zkAddress;

    private String serviceName;

    @Value("${server.port}")
    private String port;

    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private ServiceInfo serviceInfo;

    @SneakyThrows
    public ServiceInfo getServiceInfo() {
        if (serviceInfo == null) {
            synchronized (this) {
                if (serviceInfo == null) {
                    InetAddress localHost = Inet4Address.getLocalHost();
                    String ip = localHost.getHostAddress();

                    this.serviceInfo = (ServiceInfo
                            .builder()
                            .name(serviceName)
                            .address(ip + ":" + port)
                            .build());
                }
            }
        }

        return serviceInfo;
    }
}
